package com.topvision.camerasdk.album.utils;

import android.os.SystemClock;
import android.view.MotionEvent;

import com.topvision.camerasdk.album.view.PhotoView;

/**
 * 模拟事件工具类(目前使用在图片放大缩小)
 * User: jack(jackgu@topvision-cv.com)
 * Date: 2016-12-08
 * Time: 11:25
 */
public class MotionEventUtil {
    //x轴得增量
    private static final int X_VALUE = 20;
    //y轴移动增量
    private static final int Y_VALUE = 20;

    /**
     * 模拟上滑
     *
     * @author jack
     * create at 2016/12/8 10:25
     */
    public static void simulateMoveDown(PhotoView view, int x, int y) {
        long dowTime = SystemClock.uptimeMillis();
        final MotionEvent downEvent = MotionEvent.obtain(dowTime, dowTime,
                MotionEvent.ACTION_DOWN, x, y, 0);
        final MotionEvent moveEvent1 = MotionEvent.obtain(dowTime, dowTime,
                MotionEvent.ACTION_MOVE, x, y, 0);
        final MotionEvent moveEvent2 = MotionEvent.obtain(dowTime, dowTime + 20,
                MotionEvent.ACTION_MOVE, x, y - Y_VALUE, 0);
        final MotionEvent moveEvent3 = MotionEvent.obtain(dowTime, dowTime + 30,
                MotionEvent.ACTION_MOVE, x, y - Y_VALUE * 2, 0);
        final MotionEvent moveEvent4 = MotionEvent.obtain(dowTime, dowTime + 40,
                MotionEvent.ACTION_MOVE, x, y - Y_VALUE * 3, 0);
        final MotionEvent upEvent = MotionEvent.obtain(dowTime, dowTime + 40,
                MotionEvent.ACTION_UP, x, y - Y_VALUE * 3, 0);
        view.dispatchTouchEvent(downEvent);
        view.dispatchTouchEvent(moveEvent1);
        view.dispatchTouchEvent(moveEvent2);
        view.dispatchTouchEvent(moveEvent3);
        view.dispatchTouchEvent(moveEvent4);
        view.dispatchTouchEvent(upEvent);
        downEvent.recycle();
        upEvent.recycle();
    }

    /**
     * 模拟向下滑动事件
     *
     * @author jack
     * create at 2016/12/8 10:23
     */
    public static void simulateMoveUp(PhotoView view, int x, int y) {
        long dowTime = SystemClock.uptimeMillis();
        final MotionEvent downEvent = MotionEvent.obtain(dowTime, dowTime,
                MotionEvent.ACTION_DOWN, x, y, 0);
        final MotionEvent moveEvent1 = MotionEvent.obtain(dowTime, dowTime,
                MotionEvent.ACTION_MOVE, x, y, 0);
        final MotionEvent moveEvent2 = MotionEvent.obtain(dowTime, dowTime + 20,
                MotionEvent.ACTION_MOVE, x, y + Y_VALUE, 0);
        final MotionEvent moveEvent3 = MotionEvent.obtain(dowTime, dowTime + 30,
                MotionEvent.ACTION_MOVE, x, y + Y_VALUE * 2, 0);
        final MotionEvent moveEvent4 = MotionEvent.obtain(dowTime, dowTime + 40,
                MotionEvent.ACTION_MOVE, x, y + Y_VALUE * 3, 0);
        final MotionEvent upEvent = MotionEvent.obtain(dowTime, dowTime + 40,
                MotionEvent.ACTION_UP, x, y + Y_VALUE * 3, 0);
        view.dispatchTouchEvent(downEvent);
        view.dispatchTouchEvent(moveEvent1);
        view.dispatchTouchEvent(moveEvent2);
        view.dispatchTouchEvent(moveEvent3);
        view.dispatchTouchEvent(moveEvent4);
        view.dispatchTouchEvent(upEvent);
        downEvent.recycle();
        upEvent.recycle();
    }


    /**
     * 模拟滑动左滑事件
     *
     * @param view 图片控件
     * @param x    起点坐标x
     * @param y    起点坐标y
     *             create at 2016/12/8 9:38
     * @author jack
     */
    public static void simulateMoveRight(PhotoView view, int x, int y) {
        long dowTime = SystemClock.uptimeMillis();
        final MotionEvent downEvent = MotionEvent.obtain(dowTime, dowTime,
                MotionEvent.ACTION_DOWN, x, y, 0);
        final MotionEvent moveEvent1 = MotionEvent.obtain(dowTime, dowTime,
                MotionEvent.ACTION_MOVE, x, y, 0);
        final MotionEvent moveEvent2 = MotionEvent.obtain(dowTime, dowTime + 20,
                MotionEvent.ACTION_MOVE, x + X_VALUE, y, 0);
        final MotionEvent moveEvent3 = MotionEvent.obtain(dowTime, dowTime + 30,
                MotionEvent.ACTION_MOVE, x + X_VALUE * 2, y, 0);
        final MotionEvent moveEvent4 = MotionEvent.obtain(dowTime, dowTime + 40,
                MotionEvent.ACTION_MOVE, x + X_VALUE * 3, y, 0);
        final MotionEvent upEvent = MotionEvent.obtain(dowTime, dowTime + 40,
                MotionEvent.ACTION_UP, x + X_VALUE * 3, y, 0);
        view.dispatchTouchEvent(downEvent);
        view.dispatchTouchEvent(moveEvent1);
        view.dispatchTouchEvent(moveEvent2);
        view.dispatchTouchEvent(moveEvent3);
        view.dispatchTouchEvent(moveEvent4);
        view.dispatchTouchEvent(upEvent);
        downEvent.recycle();
        upEvent.recycle();
    }


    /**
     * 模拟滑动左滑事件
     *
     * @param view 图片控件
     * @param x    起点坐标x
     * @param y    起点坐标y
     *             create at 2016/12/8 9:38
     * @author jack
     */
    public static void simulateMoveLeft(PhotoView view, int x, int y) {
        long dowTime = SystemClock.uptimeMillis();
        final MotionEvent downEvent = MotionEvent.obtain(dowTime, dowTime,
                MotionEvent.ACTION_DOWN, x, y, 0);
        final MotionEvent moveEvent1 = MotionEvent.obtain(dowTime, dowTime,
                MotionEvent.ACTION_MOVE, x, y, 0);
        final MotionEvent moveEvent2 = MotionEvent.obtain(dowTime, dowTime + 20,
                MotionEvent.ACTION_MOVE, x - X_VALUE, y, 0);
        final MotionEvent moveEvent3 = MotionEvent.obtain(dowTime, dowTime + 30,
                MotionEvent.ACTION_MOVE, x - X_VALUE * 2, y, 0);
        final MotionEvent moveEvent4 = MotionEvent.obtain(dowTime, dowTime + 40,
                MotionEvent.ACTION_MOVE, x - X_VALUE * 3, y, 0);
        final MotionEvent upEvent = MotionEvent.obtain(dowTime, dowTime + 40,
                MotionEvent.ACTION_UP, x - X_VALUE * 3, y, 0);
        view.dispatchTouchEvent(downEvent);
        view.dispatchTouchEvent(moveEvent1);
        view.dispatchTouchEvent(moveEvent2);
        view.dispatchTouchEvent(moveEvent3);
        view.dispatchTouchEvent(moveEvent4);
        view.dispatchTouchEvent(upEvent);
        downEvent.recycle();
        upEvent.recycle();
    }
}  